<template>
  <a-modal
    title="新增"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="充值名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入充值名称" v-decorator="['name', {rules: [{required: true, message: '请输入充值名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="充值金额"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入充值金额" v-decorator="['money', {rules: [{required: true, message: '请输入充值金额！'}]}]" />
        </a-form-item>

        <a-form-item
          label="到账钻石数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入到账钻石数量" v-decorator="['diamonds', {rules: [{required: true, message: '请输入到账钻石数量！'}]}]" />
        </a-form-item>
        <a-form-item
          label="赠送钻石数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入赠送钻石数量" v-decorator="['give', {rules: [{required: true, message: '请输入赠送钻石数量！'}]}]" />
        </a-form-item>
        <a-form-item
          label="ios内购产品id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入ios内购产品id" v-decorator="['productId', {rules: [{required: true, message: '请输入ios内购产品id！'}]}]" />
        </a-form-item>
        <a-form-item
          label="状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select  style="width: 100%" placeholder="请选择状态" v-decorator="['status', {rules: [{ required: true, message: '请选择状态！' }]}]" >
            <a-select-option v-for="(item,index) in rechargePackageStatusDict" :key="index" :value="item.code" >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { appDiamondPackageAdd } from '@/api/modular/app/diamond/appDiamondPackageManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        rechargePackageStatusDict: [],
        form: this.$form.createForm(this)
      }
    },
    created () {
      // 初始化字典下来
      this.sysDictTypeDropDown()
    },
    methods: {
      /**
       * 获取字典数据
       */
      sysDictTypeDropDown () {
        this.rechargePackageStatusDict = this.$options.filters['dictData']('recharge_package_status')
      },
      // 初始化方法
      add (record) {
        this.visible = true
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            appDiamondPackageAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
